package com.wk.warehouse.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.entity.Product;
import com.wk.warehouse.entity.Transshipment;
import com.wk.warehouse.entity.User;
import com.wk.warehouse.mapper.ProductMapper;
import com.wk.warehouse.mapper.TransshipmentMapper;
import com.wk.warehouse.page.Page;
import com.wk.warehouse.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductServiceImp implements ProductService {
	
	@Autowired
	ProductMapper productMapper;
	@Autowired
	private TransshipmentMapper transshipmentMapper;
	
	/**
	 * 查询商品信息
	 */
	@Override
	public List<Product> selectProductMess(Product product, Page page) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("product", product);
		map.put("page", page);
		List<Product> productList = productMapper.selectProductMess(map);
		return productList;
	}
	
	/**
	 * 查询商品总数
	 */
	@Override
	public int selectCount(Product product) {
		return productMapper.selectCount(product);
	}
	
	/**
	 * 修改商品
	 */
	@Override
	public int updateProduct(Product product){
		return productMapper.updateProduct(product);
	}

	/**
	 * 修改源商品库存
	 */
	public int updateSourceInvent(int sourceProductId, int transNum){
		HashMap map = new HashMap();
		map.put("sourceProductId", sourceProductId);
		map.put("transNum", transNum);
		return productMapper.updateSourceInvent(map);
	}

	/**
	 * 修改目标商品库存
	 */
	public int updateTargetInvent(int targetProductId, int transNum){
		HashMap map = new HashMap();
		map.put("targetProductId", targetProductId);
		map.put("transNum", transNum);
		return productMapper.updateTargetInvent(map);
	}
	
	/**
	 * 添加商品
	 */
	@Override
	public int addProduct(Product product){
		return productMapper.addProduct(product);
	}

	/**
	 * 商品库存
	 */
	public int findProductInvent(int productId){
		return productMapper.findProductInvent(productId);
	}

	/**
	 * 先将结果集插入到商品表，再获取最新的商品id，最后修改插入的信息（仓库id,编号）
	 *	下面两个sql语句需用事务保证一致性，注意product_id不同（查询插入是源商品id，修改时目标商品id）
	 */
	public int insertSelectResult(int productId){
		return productMapper.insertSelectResult(productId);
	}
	public int getNewProductId(){
		return productMapper.getNewProductId();
	}
	public int updateInsertInfo(Product product){
		return productMapper.updateInsertInfo(product);
	}

	@Override
	public int changeState(Product product) {
		return productMapper.changeState(product);
	}

	/**
	 * 根据商品编号查商品信息
	 */
	public int findProductByNum(String productNum){
		return productMapper.findProductByNum(productNum);
	}

	/**
	 * 根据商品id查商品信息
	 */
	public Product selectProductById(int productId){
		return productMapper.selectProductById(productId);
	}

	/**
	 * 删除单个商品
	 */
	@Override
	public int deleteProduct(int productId) {
		return productMapper.deleteProduct(productId);
	}

	/**
	 * 删除多个商品
	 */
	@Override
	public int deleteProducts(List<Integer> productIds) {
		int updateRows = 0;
		if(productIds!=null && productIds.size()>0){
			for(int productId: productIds){
				productMapper.deleteProduct(productId);
				updateRows++;
			}
		}
		return updateRows;
	}
}
